package com.chaochis.wind.service.impl;

import com.chaochis.wind.base_exceptions.serve_exception.DataHasDeleteException;
import com.chaochis.wind.common_exceptions.has_delete_exception.UserHasTombstoneException;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotActiveException;
import com.chaochis.wind.common_exceptions.not_active_exception.UserNotActiveException;
import com.chaochis.wind.base_exceptions.serve_exception.DataNotExistException;
import com.chaochis.wind.common_exceptions.not_exist_exception.UserNotExistException;
import com.chaochis.wind.common_exceptions.not_support_exception.LoginMethodNotSupportException;
import com.chaochis.wind.common_exceptions.other_login_exceptions.UserPasswordErrorException;
import com.chaochis.wind.enums.AccountType;
import com.chaochis.wind.enums.DataStatus;
import com.chaochis.wind.service.WindAccountService;
import com.chaochis.wind.service.WindLoginService;
import com.chaochis.wind.service.WindUserService;
import com.chaochis.wind.util.id_generator.IdGenerator;
import com.chaochis.wind.vo.WindAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("windWebAdminLoginService")
public class WindWebAdminLoginServiceImpl implements WindLoginService {
    @Autowired
    @Qualifier("tokenGenerator")
    private IdGenerator tokenGenerator;

    @Autowired
    private WindAccountService windAccountService;

    @Autowired
    private WindUserService windUserService;

    /**
     * 执行得登录业务
     * @param windAccount 要登录得账号(仅accountName, accountType, password生效)
     * @return 返回登录成功得token
     * @throws DataHasDeleteException 异常: 账号已被删除
     * @throws DataNotExistException 异常: 账号不存在
     * @throws DataNotActiveException 异常: 账号未激活
     * @throws UserPasswordErrorException 异常: 密码错误，登录失败
     * @throws LoginMethodNotSupportException 异常: 不支持的登录方式
     */
    @Override
    public String login(WindAccount windAccount) throws DataHasDeleteException, DataNotExistException, DataNotActiveException, UserPasswordErrorException, LoginMethodNotSupportException {
        // 管理后端登录支持用户名登录，邮箱登录, 手机号登录
        return switch (windAccount.getAccountType()) {
            case UserName, Email, PhoneNumber -> this.loginByUserNameType(windAccount);
            default -> throw new LoginMethodNotSupportException("不支持的登录方式!");
        };
    }
    public String loginByUserNameType(WindAccount windAccount) throws DataHasDeleteException, DataNotExistException, DataNotActiveException, UserPasswordErrorException {
        var account = this.windAccountService.getWindAccountByAccountName(windAccount.getAccountName(), AccountType.UserName);
        // 如果找不到该用户，就抛出对应异常
        if (account == null) {
            throw new UserNotExistException("该用户不存在，请您检查一下是否输错了用户名？");
        }
        // 如果该用户状态不对，则也抛出异常
        if (DataStatus.Deleted.equals(account.getAccountStatus())) {
            throw new UserHasTombstoneException("该用户已被删除，请向联系管理员重新添加账号!");
        } else if (DataStatus.Inactivated.equals(account.getAccountStatus())) {
            throw new UserNotActiveException("该用户还未被激活，请联系管理员激活该账号!");
        }

        // 校验用户的密码
        var passwordIsCorrect = account.getAccountPassword().equals(windAccount.getAccountPassword());
        if (!passwordIsCorrect) {
            throw new UserPasswordErrorException("您输入的密码不正确，请重新输入密码!");
        }

        // 通过账号获取该账号绑定得用户
        var loginUser = this.windAccountService.getBoundWindUser(account.getAccountId());
        // 生成token并保存进redis中
        var token = this.tokenGenerator.generator();
        this.windUserService.saveTempUserInfoByToken(token, loginUser);
        return token;
    }
    
    @Override
    public Boolean logout() {
        return null;
    }

}
